<?php

namespace app\admin\controller;

use app\common\model\ConfigChatPloyModel;
use think\facade\Request;
use think\Validate;

class ChatployController extends BaseController
{
    /**
     * 聊天室风控配置
     * @return mixed
     */
    public function index()
    {
        $this->assign('chat_ploy', ConfigChatPloyModel::get(1));
        return $this->fetch();
    }


    public function edit_post()
    {
        $param = Request::param();

        $validate = new Validate([
            'speak_rate'              => 'require|number',
            'toggle_speak_rate'       => 'require|bool',
            'single_msg_num_length'   => 'require|number',
            'toggle_single_msg'       => 'require|bool',
            'num_msg_count'           => 'require|number',
            'toggle_num_msg_count'    => 'require|bool',
            'toggle_link_set'         => 'require|bool',
            'continue_trigger_num'    => 'require|number',
            'continue_trigger_banned' => 'require|number',
            'toggle_continue_trigger' => 'require|bool',
            'total_trigger'           => 'require|number',
            'total_trigger_banned'    => 'require|number',
            'toggle_total_trigger'    => 'require|bool',
        ], [
            'speak_rate'              => '发言频率填写错误',
            'toggle_speak_rate'       => '发言频率开关配置错误',
            'single_msg_num_length'   => '单条消息连续数字长度错误',
            'toggle_single_msg'       => '单条消息连续数字长度开关配置错误',
            'num_msg_count'           => '连续数字消息条数错误',
            'toggle_num_msg_count'    => '连续数字消息条数开关配置错误',
            'toggle_link_set'         => '链接限制开关配置错误',
            'continue_trigger_num'    => '连续触发风控次数错误',
            'continue_trigger_banned' => '连续触发风控禁言配置错误',
            'toggle_continue_trigger' => '连续触发风控开关配置错误',
            'total_trigger'           => '累计触发风控次数错误',
            'total_trigger_banned'    => '累计触发风控禁言配置错误',
            'toggle_total_trigger'    => '累计触发风控开关配置错误',
        ]);

        if (!$validate->check(Request::param())) {
            return self::bulidFail($validate->getError());
        }


        $chat_ploy = ConfigChatPloyModel::get(1);
        if ($chat_ploy->save($param)) {
            $this->addAdminLog('修改聊天室风控配置');
            return self::bulidSuccess();
        }
        return self::bulidFail();
    }
}